/**
 * 路由配置
 */
import Vue from 'vue';
import NProgress from 'nprogress';
import VueRouter from 'vue-router';
import { WHITE_LIST, REDIRECT_PATH, LAYOUT_PATH } from '@/config/setting';
import store from '@/store';
import { getToken } from '@/utils/token-util';
import { routes, getMenuRoutes } from './routes';
import {logout} from "@/utils/page-tab-util";

Vue.use(VueRouter);

const router = new VueRouter({
  routes,
  // mode: 'history',
  mode: 'hash', // 修改配置，解决服务器刷新页面404 ，history,hash
  scrollBehavior() {
    return { y: 0 };
  }
});

/**
 * 路由守卫
 */
router.beforeEach((to, from, next) => {
  if (!from.path.includes(REDIRECT_PATH)) {
    NProgress.start();
  }
  // 判断是否登录
  if (getToken()) {
    // 还未注册动态路由则先获取
    if (!store.state.user.menus) {
      if(store.state.signature.salt == '') {
        store.dispatch('signature/fetchSalt').then(() => {
          store.dispatch('user/fetchUserInfo')
            .then(({ menus, homePath }) => {
              if (menus) {
                /*router.addRoute(getMenuRoutes(menus, homePath));*/
                /*修改start*/
                getMenuRoutes(menus, homePath).forEach((r)=>{
                  router.addRoute(r)
                })
                /*修改end*/
                next({ ...to, replace: true });
              }
            })
            .catch((e) => {
              console.error(e);
              next();
            });
        }).catch(e => {
          console.error(e);
          logout()
          next({
            path: '/login',
            query: to.path === LAYOUT_PATH ? {} : { from: to.path }
          });
        })
      } else {
        store.dispatch('user/fetchUserInfo')
          .then(({ menus, homePath }) => {
            if (menus) {
              /*router.addRoute(getMenuRoutes(menus, homePath));*/
              /*修改start*/
              getMenuRoutes(menus, homePath).forEach((r)=>{
                router.addRoute(r)
              })
              /*修改end*/
              next({ ...to, replace: true });
            }
          })
          .catch((e) => {
            console.error(e);
            next();
          });
      }
    } else {
      next();
    }
  } else if (WHITE_LIST.includes(to.path)) {
    next();
  } else {
    next({
      path: '/login',
      query: to.path === LAYOUT_PATH ? {} : { from: to.path }
    });
  }
});

router.afterEach((to) => {
  if (!to.path.includes(REDIRECT_PATH) && NProgress.isStarted()) {
    setTimeout(() => {
      NProgress.done(true);
    }, 200);
  }
});

export default router;
